package com.game.element.cooldown
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.utils.Dictionary;

	public class SpriteUtil
	{
		private static var coolDownBitmapData:Dictionary = new Dictionary;
		
		public function SpriteUtil()
		{
		}
		
		public static function getCoolDownBitMap(angle:Number,length:int):BitmapData{
			var angle1:int = int(angle%360);
			var obj:BitmapData = coolDownBitmapData[angle1+"_"+length] as BitmapData;
			
			if(obj == null){
				obj = draw(angle1,length,length);
				coolDownBitmapData[angle1+"_"+length] = obj;
			} 
			return obj;
		}
		
		public static function draw(angle:Number,w:int,h:int):BitmapData{
			var bitmapData:BitmapData = new BitmapData(w,h,true,0xFF0000);
			
			var shape:Shape = new Shape;
			shape.graphics.clear();
			shape.graphics.beginFill(0,0.4);
			var h1:Number = 0
			angle = angle%360;
			if(angle>0&& angle<=45){
				h1 = h/2*Math.tan(Math.PI/180*angle);
				shape.graphics.moveTo(w/2,h/2);
				shape.graphics.lineTo(w/2,0);
				shape.graphics.lineTo(0,0);
				shape.graphics.lineTo(0,h);
				shape.graphics.lineTo(w,h);
				shape.graphics.lineTo(w,0);
				shape.graphics.lineTo(w/2+h1,0);
				shape.graphics.lineTo(w/2,h/2);
				
//				shape.graphics.moveTo(w/2,h/2);
//				shape.graphics.lineTo(w/2,0);
//				shape.graphics.lineTo(w/2+h1,0);
//				shape.graphics.lineTo(w/2,h/2);
			}else if(angle>45&& angle<=90){
				h1 = h/2*Math.tan(Math.PI/180*(90-angle));
				
				shape.graphics.moveTo(w/2,h/2);
				shape.graphics.lineTo(w/2,0);
				shape.graphics.lineTo(0,0);
				shape.graphics.lineTo(0,h);
				shape.graphics.lineTo(w,h);
				shape.graphics.lineTo(w,h/2-h1);
				shape.graphics.moveTo(w/2,h/2);
				
//				shape.graphics.moveTo(w/2,h/2);
//				shape.graphics.lineTo(w/2,0);
//				shape.graphics.lineTo(w,0);
//				shape.graphics.lineTo(w,h/2-h1);
//				shape.graphics.lineTo(w/2,h/2);
			}else if(angle>90&& angle<=135){
				h1 = w/2*Math.tan(Math.PI/180*(angle-90));
				
				shape.graphics.moveTo(w/2,h/2);
				shape.graphics.lineTo(w/2,0);
				shape.graphics.lineTo(0,0);
				shape.graphics.lineTo(0,h);
				shape.graphics.lineTo(w,h);
				shape.graphics.lineTo(w,h/2+h1);
				shape.graphics.moveTo(w/2,h/2);
				
//				shape.graphics.moveTo(w/2,h/2);
//				shape.graphics.lineTo(w/2,0);
//				shape.graphics.lineTo(w,0);
//				shape.graphics.lineTo(w,h/2+h1);
//				shape.graphics.lineTo(w/2,h/2);
			}else if(angle>135&& angle<=180){
				h1 = h/2*Math.tan(Math.PI/180*(180-angle));
				
				shape.graphics.moveTo(w/2,h/2);
				shape.graphics.lineTo(w/2,0);
				shape.graphics.lineTo(0,0);
				shape.graphics.lineTo(0,h);
				shape.graphics.lineTo(w/2+h1,h);
				shape.graphics.moveTo(w/2,h/2);
				
				
//				shape.graphics.moveTo(w/2,h/2);
//				shape.graphics.lineTo(w/2,0);
//				shape.graphics.lineTo(w,0);
//				shape.graphics.lineTo(w,h);
//				shape.graphics.lineTo(w/2+h1,h);
//				shape.graphics.lineTo(w/2,h/2);
			}else if(angle>180&& angle<=225){
				h1 = h/2*Math.tan(Math.PI/180*(angle-180));
				
				shape.graphics.moveTo(w/2,h/2);
				shape.graphics.lineTo(w/2,0);
				shape.graphics.lineTo(0,0);
				shape.graphics.lineTo(0,h);
				shape.graphics.lineTo(w/2-h1,h);
				shape.graphics.moveTo(w/2,h/2);
//				
//				shape.graphics.moveTo(w/2,h/2);
//				shape.graphics.lineTo(w/2,0);
//				shape.graphics.lineTo(w,0);
//				shape.graphics.lineTo(w,h);
//				shape.graphics.lineTo(w/2-h1,h);
//				shape.graphics.lineTo(w/2,h/2);
			}else if(angle>225&& angle<=270){
				h1 = w/2*Math.tan(Math.PI/180*(270-angle));
				
				shape.graphics.moveTo(w/2,h/2);
				shape.graphics.lineTo(w/2,0);
				shape.graphics.lineTo(0,0);
				shape.graphics.lineTo(0,h/2+h1);
				shape.graphics.moveTo(w/2,h/2);
				
//				
//				shape.graphics.moveTo(w/2,h/2);
//				shape.graphics.lineTo(w/2,0);
//				shape.graphics.lineTo(w,0);
//				shape.graphics.lineTo(w,h);
//				shape.graphics.lineTo(0,h);
//				shape.graphics.lineTo(0,h/2+h1);
//				shape.graphics.lineTo(w/2,h/2);
			}else if(angle>270&& angle<=315){
				h1 = h/2*Math.tan(Math.PI/180*(angle-270));
				
				shape.graphics.moveTo(w/2,h/2);
				shape.graphics.lineTo(w/2,0);
				shape.graphics.lineTo(0,0);
				shape.graphics.lineTo(0,h/2-h1);
				shape.graphics.moveTo(w/2,h/2);
//				
//				shape.graphics.moveTo(w/2,h/2);
//				shape.graphics.lineTo(w/2,0);
//				shape.graphics.lineTo(w,0);
//				shape.graphics.lineTo(w,h);
//				shape.graphics.lineTo(0,h);
//				shape.graphics.lineTo(0,h/2-h1);
//				shape.graphics.lineTo(w/2,h/2);
			}else if(angle>315&& angle<=360){
				h1 = h/2*Math.tan(Math.PI/180*(360-angle));
				
				shape.graphics.moveTo(w/2,h/2);
				shape.graphics.lineTo(w/2,0);
				shape.graphics.lineTo(w/2-h1,0);
				shape.graphics.moveTo(w/2,h/2);
//				
//				shape.graphics.moveTo(w/2,h/2);
//				shape.graphics.lineTo(w/2,0);
//				shape.graphics.lineTo(w,0);
//				shape.graphics.lineTo(w,h);
//				shape.graphics.lineTo(0,h);
//				shape.graphics.lineTo(0,0);
//				shape.graphics.lineTo(w/2-h1,0);
//				shape.graphics.lineTo(w/2,h/2);
			}
			
			shape.graphics.endFill();
			bitmapData.draw(shape);
			return bitmapData;
		}
	}
}